/*================================================================
 *   Copyright (C) 2021 baichao All rights reserved.
 *
 *   文件名称：shutdownService.cpp
 *   创 建 者：baichao
 *   创建日期：2021年02月08日
 *   描    述：
 *
 ================================================================*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>



 
int main(){
    //创建套接字
    int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
    //将套接字和IP、端口绑定
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));  //每个字节都用0填充
    serv_addr.sin_family = AF_INET;  //使用IPv4地址
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //具体的IP地址
    serv_addr.sin_port = htons(11230);  //端口
    bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
 
    //进入监听状态，等待用户发起请求
    listen(serv_sock, SOMAXCONN);
 
 
    //接收客户端请求
    struct sockaddr_in clnt_addr;
    socklen_t clnt_addr_size = sizeof(clnt_addr);
 
    int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
    int count = 0;
    int size = 3;
    while(size--)
    {
 
        //sleep(5);
        char str[100];
        int readNum = read(clnt_sock,str,sizeof(str));
        if(readNum <= 0)
            continue;
        std::cout<<"第"<<++count<<"次读取数据长度为："<<readNum<<std::endl;
        // write(clnt_sock, , sizeof(str));
 
    }
    shutdown(clnt_sock,SHUT_RD);
 
    size = 3;
    while(size--)
    {
 
        //sleep(5);
        char str[100];
        int readNum = read(clnt_sock,str,sizeof(str));
        if(readNum <= 0)
            continue;
        std::cout<<"第"<<++count<<"次读取数据长度为："<<readNum<<std::endl;
        // write(clnt_sock, , sizeof(str));
 
    }
    close(clnt_sock);
    close(serv_sock);
    return 0;
}